package com.haitao.jt808.entity.gpsdata;

import com.haitao.jt808.util.Jt808Constant;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import lombok.Data;

import java.io.UnsupportedEncodingException;

/**
 * @author: sanhoitou[sanhoitou@hotmail.com]
 * @Description: 平台通用应答
 * @Version: 1.0
 */
@Data
public class Data0x8300 extends DataPackage {

    /**
     * 成功/确认
     */
    public static final byte SUCCESS = 0;

    /**
     * 失败
     */
    public static final byte FAILURE = 1;

    /**
     * 消息有误
     */
    public static final byte MSG_ERROR = 2;

    /**
     * 不支持
     */
    public static final byte UNSUPPORTED = 3;

    /**
     * 报警处理确认
     */
    public static final byte ALARM_PROCESS_ACK = 4;

    public static Data0x8300 response(String terminalPhone, int flowId, String text) {
        Data0x8300 resp = new Data0x8300();
        Header respHeader = resp.getHeader();
        //设置消息ID
        respHeader.setMsgId(Jt808Constant.文本信息下发);
        //设置消息流水号
        respHeader.setFlowId(flowId);
        respHeader.setTerminalPhone(terminalPhone);
        // 消息体
        // 计算消息体字节数
        int length = text.length() * 2;
        ByteBuf byteBuf = ByteBufAllocator.DEFAULT.heapBuffer(length + 1);
        //标志0x08、0x09终端 TTS 播读
        // 0x01：指令下发:
        // #2014*SET*T:shto.net,17018#
        // #2014*SET*DS:shto.net,10000,root1,123,TEST_RTCM32#
        byteBuf.writeByte(0x09);
        //文本信息进行gbk编码
        try {
            byte[]  gbk = text.getBytes("GBK");
            byteBuf.writeBytes(gbk);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        //设置返回的消息
        resp.setMsgBody(byteBuf);
        return resp;
    }

}
